/*
 * @Author: your name
 * @Date: 2021-07-02 19:43:26
 * @LastEditTime: 2021-07-28 14:43:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \TEST\Core\mycode\jy61.c
 */
#include "jy61.h"
#include "usart.h"
JY61_data jy61_data;
JY61_RX jy61_rx;
void receive_jy61_data(UART_HandleTypeDef *huart)
{
	static int if_error_now = 1;
    static int if_error_last = 0;
    if (huart == &huart6)
    {
            if(jy61_rx.a_buffer[0] !=0x55)
            {
                if(if_error_last == 1)
                {
                    HAL_UART_Receive_IT(&huart6, (uint8_t *)&jy61_rx.a_buffer[0], 33);
                    if_error_last = 0;
                    return;
                } 
                if_error_now = 1;
                HAL_UART_Receive_IT(&huart6, (uint8_t *)&jy61_rx.a_buffer[0], 1);
                return;
            }
            else
            {//之前还是错误的
                if(if_error_now == 1)
                {
                    HAL_UART_Receive_IT(&huart6, (uint8_t *)&jy61_rx.a_buffer[0], 32);
                    if_error_now =0;
                    if_error_last = 1;
                    return;
                } 
                //  HAL_UART_Transmit(&huart3,&jy61_rx.a_buffer[0],33,100);
                JY61_data_process();
                HAL_UART_Receive_IT(&huart6, (uint8_t *)&jy61_rx.a_buffer[0], 33);
            }
               
    }
}
/**
 * @description:将串口收集到的数据转换成实际的角度角速度等数据并存放到结构体中 ,该函数需要在主函数的循环中不断执行
 * 当接收完一次数据后，会将finished标志重新变为0，缓冲区中才能写入新的数据
 * 只有当finished标志为1时，这个函数才有效，将缓冲区中的数据进行解析。
 * @param {*}
 * @return {*}
 */
void JY61_data_process(void)
{
    //printf("usart3_jy61.finished = %d\n",usart3_jy61.finished);

        for (int i = 0; i < 3; i++)
        {
        if(jy61_rx.a_buffer[0+11*i]==0x55) //检查帧头
        {
            switch(jy61_rx.a_buffer[1+11*i])
            {
                case 0x51:
                {   
                    if((i!=2 &&(jy61_rx.a_buffer[1+11*(i+1)]==0x51|jy61_rx.a_buffer[1+11*(i+1)]==0x52|jy61_rx.a_buffer[1+11*(i+1)]==0x53))|i==2)
                    {
                    jy61_data.a[0+11*i] = ((jy61_rx.a_buffer [3+11*i]<<8| jy61_rx.a_buffer [2+11*i]))/32768.0*16;
                    jy61_data.a[1+11*i] = ((jy61_rx.a_buffer [5+11*i]<<8| jy61_rx.a_buffer [4+11*i]))/32768.0*16;
                    jy61_data.a[2+11*i] = ((jy61_rx.a_buffer [7+11*i]<<8| jy61_rx.a_buffer [6+11*i]))/32768.0*16;
                    }

                    break;
                }
                case 0x52:
                {
                    if((i!=2 &&(jy61_rx.a_buffer[1+11*(i+1)]==0x51|jy61_rx.a_buffer[1+11*(i+1)]==0x52|jy61_rx.a_buffer[1+11*(i+1)]==0x53))|i==2)
                    { 
                    jy61_data.w[0] = ((jy61_rx.a_buffer [3+11*i]<<8| jy61_rx.a_buffer [2+11*i]))/32768.0*2000;
                    jy61_data.w[1] = ((jy61_rx.a_buffer [5+11*i]<<8| jy61_rx.a_buffer [4+11*i]))/32768.0*2000;
                    jy61_data.w[2] = ((jy61_rx.a_buffer [7+11*i]<<8| jy61_rx.a_buffer [6+11*i]))/32768.0*2000;
                    }
                break;
                }
                case 0x53:
                {
                    if((i!=2 &&(jy61_rx.a_buffer[1+11*(i+1)]==0x51|jy61_rx.a_buffer[1+11*(i+1)]==0x52|jy61_rx.a_buffer[1+11*(i+1)]==0x53))|i==2)
                    { 
                    jy61_data.angle[0] = ((jy61_rx.a_buffer [3+11*i]<<8| jy61_rx.a_buffer [2+11*i]))/32768.0*180;
                    jy61_data.angle[1] = ((jy61_rx.a_buffer [5+11*i]<<8| jy61_rx.a_buffer [4+11*i]))/32768.0*180;
                    jy61_data.angle[2] = ((jy61_rx.a_buffer [7+11*i]<<8| jy61_rx.a_buffer [6+11*i]))/32768.0*180;
                    for (int i = 0; i < 3; i++)
                    {
                        if (jy61_data.angle[i]>180)
                        {
                            jy61_data.angle[i] = jy61_data.angle[i] - 360;
                        }
                        
                    }
                    
                    }
                break;
                }
                default:
                {
                break;
                }
            }
        }
        }

    }

